package graph;



import static java.util.Calendar.DAY_OF_WEEK;
import static java.util.Calendar.SATURDAY;
import static java.util.Calendar.SUNDAY;

import java.awt.Color;
import java.awt.Font;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;

import stock.Sale;
import ernie.database.Database;


public class HighLowDiagram extends Diagram {
	private long lastInMillis = -10;
	private boolean hasBefore = false;

	
	public HighLowDiagram(String title, int width, int heigth, boolean visible) {
		super(title, width, heigth, visible);
		
	}
	
	public void drawNextLine(Color color, int middleX, int middleY, int size,
			long amount, double rate, long time, int circleSize,
			boolean circleDot) {

		chache.add(new Dot(middleX, middleY, amount, color, rate, time,
				circleSize, circleDot));
		if (hasChangedAmount) {
			changeCircle();

		} else if (hasChangedRate) {
			changeRate();
		} else {
			
			if (lastX != 0 && lastY != 0) {
				if(time - lastInMillis ==1 ) {
					if (hasBefore) {
						
						drawFatLine(Color.BLACK, middleX, middleY, middleX+2, middleY);
						hasBefore = false;
						
					} else {
						hasBefore = true;
					}
					
					drawLine(Color.BLACK, middleX, middleY, lastX, lastY);
					
				} else {
					hasBefore = false;
				}
			}
			lastInMillis = time;
			lastX = middleX;
			lastY = middleY;
		}

	}
	
	public void drawFatLine(Color color, int x1, int y1, int x2, int y2) {

		synchronized (drawables) {
			drawables.add(new Line(color, x1, y1, x2, y2));
			drawables.add(new Line(color, x1, y1+1, x2, y2+1));
			//drawables.add(new Line(color, x1, y1-1, x2, y2-1));
		}
	}

	protected void changeRate() {

		hasChangedRate = false;
		clear();
		drawAxes();
		// drawOrderDepth();
		lastX = 0;
		lastY = 0;
		drawRateLines();
		drawTimeLines();
		for (Dot d : chache) {
			if (!outside(d.getTime())) {
				int y = getY(d.getRate());
				d.setY(y);
				if (lastX != 0) {
					
					if(d.getTime() - lastInMillis ==1 ) {
						if (hasBefore) {
							drawFatLine(d.getColor(),d.getX(), d.getY(), d.getX()+2,d.getY());
							hasBefore = false;
							
						} else {
							hasBefore = true;
						}
						
						drawLine(d.getColor(), d.getX(), d.getY(), lastX, lastY);
						
					} else {
						hasBefore = false;
					}

				}
				lastInMillis = d.getTime();
				lastX = d.getX();
				lastY = d.getY();
				
			}
		}

	}
	
	protected void changeCircle() {
		hasChangedAmount = false;
		clear();
		drawAxes();
		// drawOrderDepth();
		lastX = 0;
		lastY = 0;
		drawRateLines();
		drawTimeLines();
		hasBefore = false;
		for (Dot d : chache) {
			if (!outside(d.getTime())) {
				if (lastX != 0) {
					
					if(d.getTime() - lastInMillis ==1 ) {
						if (hasBefore) {
							drawFatLine(d.getColor(),d.getX(), d.getY(), d.getX()+2,d.getY());
							hasBefore = false;
							
						} else {
							hasBefore = true;
						}
						
						drawLine(d.getColor(), d.getX(), d.getY(), lastX, lastY);
						
					} else {
						hasBefore = false;
					}
				}
				lastX = d.getX();
				lastY = d.getY();
				
				lastInMillis = d.getTime();
			}
		}

	}
	
	protected void redrawNewTimePeriod() {
		clear();
		drawAxes();
		// drawOrderDepth();
		drawRateLines();
		drawTimeLines();
		options.latestTime = options.startDate.getTimeInMillis() - 1;
		options.lastSale = null;
		lastX = 0;
		lastY = 0;
		

		

	}
	
	public void setDot(long timeInMillis, double price, long amount,
			boolean circleDot) {
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(timeInMillis);

		if (c.get(DAY_OF_WEEK) != SATURDAY && c.get(DAY_OF_WEEK) != SUNDAY
				&& c.getTimeInMillis() >= options.startDate.getTimeInMillis()
				&& c.getTimeInMillis() <= options.endDate.getTimeInMillis()) {
			
			//if(circleDot) {
				
			//	addMostCommon(price);
			//}
			options.latestTime = timeInMillis;
			CheckIfAmountIntervalIsChanged(amount);

			int y = getY(price);
			int x = getX(timeInMillis);
			int size = calculateSize(amount);
			
			

			drawNextLine(Color.BLACK, x, y, graph.Options.dotSize, amount,
					price, timeInMillis, size, circleDot);
			
			y = getY(price);
			
			

			

		}

	}
	
	
	/**
	  * Updates this diagram with new values
	  * @param db the database to fetch data from
	  */
	 public boolean setValues(Database db) {
		 	
	        List<Sale> list = db.loadEntries(getLatestTime(), getTitle());
	        Collections.sort(list);
	        for (Sale s : list) {
	            if (s.getTime() > getLatestTime()) {
	                setDot(s);
	            }
	        }
	        
	        return true;
	    }

	
	 
	
	

}
